package com.da.javatest.thread.juc.base;

import java.util.concurrent.ExecutionException;
import java.util.concurrent.FutureTask;

/**
 * @author JokerDa.
 * @date 2021/7/8 10:57
 * Description:
 */
public class ThreadDemo4 {
    public static void main(String[] args) throws ExecutionException, InterruptedException {
        FutureTask<Integer> taskA = new FutureTask<>(() -> {
            System.out.println("task A");
            return 1024;
        });

        FutureTask<Integer> taskB = new FutureTask<>(() -> {
            System.out.println("task B");
            int i = 1 / 0;
            return 618;
        });

        new Thread(taskA).start();
        new Thread(taskB).start();

        while (!taskA.isDone()) {
            System.out.print("wait...");
        }
        System.out.println(taskA.get());
        // 结果计算一次就行，后面直接返回
        while (!taskA.isDone()) {
            System.out.print("wait...");
        }
        System.out.println(taskA.get());

        try {
            Integer integer = taskB.get();
            System.out.println(integer);
        } catch (Exception e) {
            e.printStackTrace();
        }

        System.out.println(Thread.currentThread().getName()+" is done!");
    }
}
